package com.hypertech.core.ffmpeg.videoDao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;


@Repository
@Slf4j
public class VideoDao{
    private boolean flag = false;
    @Value("${ffmpeg.workSpace}")
    private String workSpace;
    public boolean runCmd(List<String> command) {
        ProcessBuilder builder = new ProcessBuilder();
        builder.directory(new File(workSpace));/*切换工作目录，不加这一句还真不行，此目录为你ffmpeg文件夹的存放目录*/
        log.warn("================================================================================");
        log.info("start convert...");
        log.warn("___+++ffmpeg+++___工作路径："+builder.directory());
        builder.command(command);
        try {
            builder.redirectErrorStream(true);
            log.warn(builder.command().toString().replace(","," "));//打印命令
            Process process = builder.start();
            InputStream inputStream = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(inputStreamReader);
            new Thread(){
                public void run(){
                    try {
                        String line = "";
                        int index = 0;
                        while ((line = br.readLine()) != null) {
                            if (flag == false){
                                if (line.contains("frame=")){ //成功的转码过程getErrorStream包含frame=
                                    flag=true;
                                }
                            }
                            index++;
                            if (index%50==0)
                                log.warn(line);
                        }
                        if (br != null) {
                            br.close();
                        }
                        if (inputStreamReader != null) {
                            inputStreamReader.close();
                        }
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        log.info("convert finish");
                        log.warn("convert result: "+flag);
                        log.warn("================================================================================");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            //可能导致进程阻塞，甚至死锁
            int ret = process.waitFor();
            log.warn("process.waitFor() value:"+ret);
            log.warn("exitValue:"+process.exitValue());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return  flag;
        }
    }


}
